//
//  LZFEssenceController.m
//  LZFBuDieJie
//
//  Created by 梁梓烽 on 16/6/21.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

#import "LZFEssenceController.h"
#import "UIImage+Render.h"
#import "LZFCustomButton.h"

#import "LZFAllVC1.h"
#import "LZFVedioVC.h"
#import "LZFSoundsVC.h"
#import "LZFPicVC.h"
#import "LZFWordVC.h"


@interface LZFEssenceController ()<UIScrollViewDelegate>
@property(nonatomic,weak)UIView *titleView;
@property(nonatomic,weak)UIView *underLineView;
@property(nonatomic,weak)UIScrollView *scrollview;
@property(nonatomic,strong)UIButton *previousSelectedButton;
@end
@implementation LZFEssenceController

-(void)viewDidLoad
{
    //设置导航条内容
    [self  setUpNavgationBar];
    
    //设置scrollview
    [self  setUpScrollView];
    
    //设置titleView
    [self setUpTitleView];
    
    //添加子控制器
    [self addChildViewControllers];
    
    
    //添加子控制器的view到scrollview上
    [self addChildView:0];
    

    
}

//设置导航条内容
-(void)setUpNavgationBar
{
    self.view.backgroundColor=[UIColor blueColor];
    
    self.navigationItem.leftBarButtonItem=[UIBarButtonItem buttonItemWith:@"nav_item_game_icon" highlightImage:@"nav_item_game_click_icon" target:self selector:@selector(click)];
    
    self.navigationItem.rightBarButtonItem=[UIBarButtonItem buttonItemWith:@"navigationButtonRandom" highlightImage:@"navigationButtonRandomClick" target:self selector:@selector(click2)];
    
    
    UIImageView *iamgeView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MainTitle"]];
    self.navigationItem.titleView=iamgeView;
    
}


//设置scrollview
-(void)setUpScrollView
{
    UIScrollView *scrollview=[[UIScrollView alloc]init];
    self.scrollview=scrollview;
    scrollview.frame=self.view.bounds;
    scrollview.contentSize=CGSizeMake(5*screenW, 0);
    scrollview.pagingEnabled=YES;
    scrollview.showsHorizontalScrollIndicator=NO;
    scrollview.showsHorizontalScrollIndicator=NO;
    scrollview.delegate=self;
    scrollview.scrollsToTop=NO;  //取消scrollsToTop属性
    //取消系统为tabbarcontroller下的scrollview增加的额外的insets,目的是为了不让导航条挡住scrollview的内容
    self.automaticallyAdjustsScrollViewInsets=NO;
    
    [self.view addSubview:scrollview];
}


//设置titleView
-(void)setUpTitleView
{
    UIView *titleView=[[UIView alloc]init];
    self.titleView=titleView;
    titleView.frame=CGRectMake(0, 64, screenW, 35);
    titleView.backgroundColor=[UIColor colorWithRed:1 green:1 blue:1 alpha:0.5];
    [self.view addSubview:titleView];
    
    //设置标题按钮
    [self seteUpTitleButton];
    
    //设置标题按钮的下划线
    [self setUnderLine];
}

  //设置标题按钮
-(void)seteUpTitleButton
{
    NSArray *titleArray=@[@"全部",@"视频",@"声音",@"图片",@"段子"];
    CGFloat buttonW=self.titleView.xmg_width/5;
    CGFloat buttonH=35;
    
    for (int i=0; i<titleArray.count; i++) {
        LZFCustomButton *button=[[LZFCustomButton alloc]init];
        button.tag=i;
        if (i==0&&self.previousSelectedButton==nil) {
            self.previousSelectedButton=button;
        }
        [self.titleView addSubview:button];
        [button setTitle:titleArray[i] forState:UIControlStateNormal];
        button.frame=CGRectMake(i*buttonW, 0, buttonW, buttonH);
        [button addTarget:self action:@selector(clickTitleButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    
}

//点击标题按钮
-(void)clickTitleButton:(UIButton *)currentButton
{
    //判断是不是重复点击按钮
   
    if (self.previousSelectedButton==currentButton) {
        //发送重复点击按钮通知
        [[NSNotificationCenter defaultCenter]postNotificationName: LZFTitleButtonDidClickedNotification object:nil];
    }
    
    //切换标题按钮的选中状态
    self.previousSelectedButton.selected=NO;
    currentButton.selected=YES;
    self.previousSelectedButton=currentButton;
    
  
    //动画切换下划线的位置
    [UIView animateWithDuration:0.5 animations:^{
        //切换下划线的位置
        self.underLineView.xmg_centerX=currentButton.xmg_centerX;
    } completion:^(BOOL finished) {
        //切换scrollview
        NSInteger index=currentButton.tag;
        [self.scrollview setContentOffset:CGPointMake(index*screenW, 0)];
        [self addChildView:index];
    }];
    
    
     [self setChildControllerViewscrollsToTop:currentButton];
    
}

#pragma mark -实现点击状态栏返回顶部功能, 设置每个子控制器的view的scrollsToTop属性,只有选中标题对应的tableview的scrollsToTop属性为yes,其余为no
-(void)setChildControllerViewscrollsToTop:(UIButton *)currentButton
{
    NSInteger buttonTag= currentButton.tag;
    for (int i=0; i<self.childViewControllers.count; i++) {
        
        //当数组中控制器的view还没加载/控制器的view的类型不是UIScrollView,过滤掉
        UIViewController *vc=self.childViewControllers[i];
        if (![vc isViewLoaded])continue;
        if (![vc.view isKindOfClass:[UIScrollView class]])return;
        
        //vc的view是UIScrollView类型,设置选中按钮对应的vc的view的scrollToTop YES,非选中按钮对应的vc的view的scrollToTop NO
        ((UIScrollView *)vc.view).scrollsToTop=(i==buttonTag);
        
    }
}


   //设置标题按钮的下划线
-(void)setUnderLine
{
    LZFCustomButton *firstButton=self.titleView.subviews[0];
    UIView *underLineView=[[UIView alloc]init];
    self.underLineView=underLineView;
    underLineView.backgroundColor=[UIColor redColor];
    CGFloat underLineH=2;
    underLineView.frame=CGRectMake(0, self.titleView.xmg_height-underLineH, 30, underLineH);
    
    [self.titleView addSubview:underLineView];
    
    firstButton.selected=YES;
    [firstButton.titleLabel  sizeToFit];
    underLineView.xmg_width=firstButton.titleLabel.xmg_width+10;
    underLineView.xmg_centerX=firstButton.xmg_centerX;
}

 //添加子控制器
-(void)addChildViewControllers
{
    [self addChildViewController:[[LZFAllVC1 alloc]init]];
    [self addChildViewController:[[LZFVedioVC alloc]init]];
    [self addChildViewController:[[LZFSoundsVC alloc]init]];
    [self addChildViewController:[[LZFPicVC alloc]init]];
    [self addChildViewController:[[LZFWordVC alloc]init]];
}


//添加子控制器的view到scrollview上
-(void)addChildView:(NSInteger)index
{
   UIView *childView = self.childViewControllers[index].view;
    
    [self.scrollview addSubview:childView];
    
    childView.frame=CGRectMake(index*childView.xmg_width, 0, childView.xmg_width, childView.xmg_height);
}


//scrollview停止滑动时
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index=(int)scrollView.contentOffset.x/screenW;
    LZFCustomButton *button=   self.titleView.subviews[index];
 
    [self clickTitleButton:button];
    
    

}
#pragma mark -按钮的点击事件触发
-(void)click

{
    NSLog(@"click game");
}
-(void)click2

{
    NSLog(@"click 2");
}
@end
